//
//  LXUserInfoCellView.m
//  cat
//
//  Created by imera on 16/3/10.
//  Copyright © 2016年 LX. All rights reserved.
//

#import "LXUserInfoCellView.h"
#import "UIImageView+WebCache.h"

#define LXUserInfoCellViewMargin 10

@interface LXUserInfoCellView()
/** 用户头像 */
@property (nonatomic, weak) UIImageView *userPhotoImageView;
/** 用户姓名 */
@property (nonatomic, weak) UILabel *userNameLabel;
/** 用户等级 */
@property (nonatomic, weak) UIImageView *userLevelImageView;
/** 用户性别 */
@property (nonatomic, weak) UIImageView *userSexImageView;
/** 猫粮 */
@property (nonatomic, weak) UILabel *catFoodLabel;
@end

@implementation LXUserInfoCellView

- (void)setUser:(LXUser *)user
{
    if (user != nil) {
        _user = user;
    }
    [self layoutSubviews];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self){
        [self initSubViews];
        self.backgroundColor = [UIColor whiteColor];
        
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(userChange) name:@"userNameChange" object:nil];
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(userChange) name:@"userSexChange" object:nil];
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(userChange) name:@"userHeadPicChange" object:nil];
    }
    return self;
}

- (void)userChange
{
    [self layoutSubviews];
}

#pragma mark - 初始化子控件
- (void)initSubViews
{
    UIImageView *userPhotoImageView = [[UIImageView alloc]init];
    self.userPhotoImageView = userPhotoImageView;
    [self addSubview:userPhotoImageView];
    
    UILabel *userNameLabel = [[UILabel alloc]init];
    self.userNameLabel = userNameLabel;
    [self addSubview:userNameLabel];
    
    UIImageView *userLevelImageView = [[UIImageView alloc]init];
    self.userLevelImageView = userLevelImageView;
    [self addSubview:userLevelImageView];
    
    UIImageView *userSexImageView = [[UIImageView alloc]init];
    self.userSexImageView = userSexImageView;
    [self addSubview:userSexImageView];
    
    UILabel *catFoodLabel = [[UILabel alloc]init];
    self.catFoodLabel = catFoodLabel;
    [self addSubview:catFoodLabel];
}

#pragma mark - 设置子控件数据
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    /** 用户头像 */
    UIImageView *userPhotoImageView = self.userPhotoImageView;
//    NSLog(@"%@", self.user.headpic);
    [userPhotoImageView sd_setImageWithURL:[NSURL URLWithString:self.user.headpic]];

    CGFloat userPhotoImageViewX = LXUserInfoCellViewMargin;
    CGFloat userPhotoImageViewY = LXUserInfoCellViewMargin * 0.5;
    CGFloat userPhotoImageViewH = (self.frame.size.height - userPhotoImageViewY * 2);
    CGFloat userPhotoImageViewW = userPhotoImageViewH;
    userPhotoImageView.frame = CGRectMake(userPhotoImageViewX, userPhotoImageViewY, userPhotoImageViewW, userPhotoImageViewH);
    
    //设置圆角
    userPhotoImageView.layer.masksToBounds = YES;
    userPhotoImageView.layer.cornerRadius = userPhotoImageViewW * 0.5;
    userPhotoImageView.layer.borderWidth = 2;
    userPhotoImageView.layer.borderColor = [UIColor whiteColor].CGColor;
    
    /** 用户姓名 */
    UILabel *userNameLabel = self.userNameLabel;
    userNameLabel.text = [self.user.uname isEqualToString:@""] ? self.user.email : self.user.uname;
    userNameLabel.font = [UIFont boldSystemFontOfSize:15];
    
    CGSize userNameLabelSize = [userNameLabel.text sizeWithAttributes:@{NSFontAttributeName:userNameLabel.font}];
    CGFloat userNameLabelX = CGRectGetMaxX(userPhotoImageView.frame) + LXUserInfoCellViewMargin;
    CGFloat userNameLabelY = userPhotoImageViewY + LXUserInfoCellViewMargin;
    CGFloat userNameLabelW = userNameLabelSize.width;
    CGFloat userNameLabelH = userNameLabelSize.height;
    userNameLabel.frame = CGRectMake(userNameLabelX, userNameLabelY, userNameLabelW, userNameLabelH);
    
    /** 用户等级 */
    UIImageView *userLevelImageView = self.userLevelImageView;
    NSString *level = [NSString stringWithFormat:@"vip_icon%ld",self.user.ulevel];
    userLevelImageView.image = [UIImage imageNamed:level];
    
    CGFloat userLevelImageViewX = CGRectGetMaxX(userNameLabel.frame) + 5;
    CGFloat userLevelImageViewY = userNameLabelY + 2;
    CGFloat userLevelImageViewW = 15;
    CGFloat userLevelImageViewH = 15;
    userLevelImageView.frame = CGRectMake(userLevelImageViewX, userLevelImageViewY, userLevelImageViewW, userLevelImageViewH);
    
    /** 用户性别 */
    UIImageView *userSexImageView = self.userSexImageView;
    if (self.user.sex == 0) {
        userSexImageView.image = [UIImage imageNamed:@"man"];
    }else{
        userSexImageView.image = [UIImage imageNamed:@"women"];
    }

    
    CGFloat userSexImageViewX = CGRectGetMaxX(userLevelImageView.frame) + 5;
    CGFloat userSexImageViewY = userNameLabelY + 1;
    CGFloat userSexImageViewW = 18;
    CGFloat userSexImageViewH = 18;
    userSexImageView.frame = CGRectMake(userSexImageViewX, userSexImageViewY, userSexImageViewW, userSexImageViewH);
    
    /** 用户猫粮数 */
    UILabel *catFoodLabel = self.catFoodLabel;
    catFoodLabel.text = [NSString stringWithFormat:@"猫粮:%ld",self.user.catfood];
    catFoodLabel.font = [UIFont systemFontOfSize:15];
    
    CGSize catFoodLabelSize = [catFoodLabel.text sizeWithAttributes:@{NSFontAttributeName:catFoodLabel.font}];
    CGFloat catFoodLabelX = userNameLabelX;
    CGFloat catFoodLabelY = CGRectGetMaxY(userNameLabel.frame) + LXUserInfoCellViewMargin;
    CGFloat catFoodLabelW = catFoodLabelSize.width;
    CGFloat catFoodLabelH = catFoodLabelSize.height;
    catFoodLabel.frame = CGRectMake(catFoodLabelX, catFoodLabelY, catFoodLabelW, catFoodLabelH);
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

@end
